package com.stgl.wxutil;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.stgl.bean.Template;

import java.util.*;


import com.stgl.bean.Template;
import com.stgl.bean.TemplateParam;
import com.stgl.util.ConfKit;
import com.stgl.wxutil.AdvancedUtil;
import com.stgl.wxutil.CommonUtil;


/**
 * @author zs
 *接口工具类
 */
public class AdvancedUtil {
	private static Logger log = LoggerFactory.getLogger(AdvancedUtil.class);
	/**
	 * 发送模板消息
	 * @param accessToken
	 * @template template
	 * @return
	 */
	public static String sendTemplateMessage(String accessToken,Template template){
		if("".equals(accessToken) || template == null){
			return "{success:false,msg:'获取数据失败！'}";
		}
		// 拼接请求地址
		String requestUrl = WxMessageUtil.xcx;
		requestUrl=requestUrl.replace("ACCESS_TOKEN", accessToken).replace("NEXT_OPENID", accessToken);
		//发送模板消息
		JSONObject jsonObject=CommonUtil.httpsRequest(requestUrl, "POST", template.toJSON());
		//如果请求成功
		int errorCode = jsonObject.getInt("errcode");
		String errorMsg = jsonObject.getString("errmsg");
		if(0==errorCode && "ok".equals(errorMsg)){
			return "{success:true,msg:'发送成功！'}";
		}else{
			return "{success:false,msg:'对不起，该用户没有关注该公众号或者已经取消了关注！'}";
		}
	}

	public static void sendTMessage(){
		//发送消息

		/*ConfKit confkit= new ConfKit("wxpay.properties");
		String accessToken = CommonUtil.getToken(confkit.get("appid"),confkit.get("appsecret")).getAccessToken();
		List<TemplateParam> tpList=new ArrayList<TemplateParam>();
		tpList.add(new TemplateParam("keyword1","2019-08-27",""));
		tpList.add(new TemplateParam("keyword2","成功",""));
		tpList.add(new TemplateParam("keyword3","牟敏燕同学",""));
		tpList.add(new TemplateParam("keyword4","522121199108163883",""));
		tpList.add(new TemplateParam("keyword5","遵义南白中心",""));

		Template template=new Template();//模板对象
		template.setTemplateId("3nAedc88zLhcr0hkYKKlwgHiVWUfCAuqfs7Oz2PT_14");//模板Id
		//发给谁
		String opendid="oAqwg5RzXoBTRAFhgGQYS-yfN4GA";
		template.setToUser(opendid);
		template.setTopColor("#FF0000");
		template.setTemplateParamList(tpList);
		template.setFormId("56ce1a350a7f4b3c8fe7514b8377ffcf");
		AdvancedUtil.sendTemplateMessage(accessToken,template);
*/

		List<TemplateParam> tpList=new ArrayList<TemplateParam>();
		tpList.add(new TemplateParam("keyword1","2019-08-27",""));
		tpList.add(new TemplateParam("keyword2","测试数据请勿回复",""));
		tpList.add(new TemplateParam("keyword3","牟敏燕同学",""));
		tpList.add(new TemplateParam("keyword4","555555555",""));
		tpList.add(new TemplateParam("keyword5","播州区直属机关幼儿园",""));
/*
		Map<String,Object>gmap=new HashMap<String,Object>();
		gmap.put("touser","oAqwg5RzXoBTRAFhgGQYS-yfN4GA");
		gmap.put("template_id","3nAedc88zLhcr0hkYKKlwgHiVWUfCAuqfs7Oz2PT_14");
		gmap.put("page","index");
		gmap.put("form_id","56ce1a350a7f4b3c8fe7514b8377ffcf");
		String jsomd=gmap.toString();
		gmap.put("data",tpList);*/

		ConfKit confkit= new ConfKit("wxpay.properties");
		String accessToken = CommonUtil.getToken(confkit.get("appid"),confkit.get("appsecret")).getAccessToken();
		String requestUrl = WxMessageUtil.xcx;
		requestUrl=requestUrl.replace("ACCESS_TOKEN", accessToken).replace("NEXT_OPENID", accessToken);

		StringBuffer buffer = new StringBuffer();
		buffer.append("{");
		buffer.append(String.format("\"touser\":\"%s\"", "oAqwg5RzXoBTRAFhgGQYS-yfN4GA")).append(",");
		buffer.append(String.format("\"template_id\":\"%s\"", "3nAedc88zLhcr0hkYKKlwgHiVWUfCAuqfs7Oz2PT_14")).append(",");
		buffer.append(String.format("\"page\":\"%s\"", "index")).append(",");
		buffer.append(String.format("\"form_id\":\"%s\"", "b5752ade45cc44f98aa1003b25d38160")).append(",");
		buffer.append("\"data\":{");
		TemplateParam param = null;
		for (int i = 0; i < tpList.size(); i++) {
			param = tpList.get(i);
			// 判断是否追加逗号
			if (i < tpList.size() - 1)
				buffer.append(String.format("\"%s\": {\"value\":\"%s\",\"color\":\"%s\"},", param.getName(), param.getValue(), param.getColor()));
			else
				buffer.append(String.format("\"%s\": {\"value\":\"%s\",\"color\":\"%s\"}", param.getName(), param.getValue(), param.getColor()));
		}
		buffer.append("}");
		buffer.append("}");
		String jjs=buffer.toString();
		JSONObject jsonObject=CommonUtil.httpsRequest(requestUrl, "POST", jjs);
		System.out.println(jsonObject);

	}


	public static void main(String args[])throws Exception{
		sendTMessage();
	}
}
